package com.fjw.wechat_tool;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.widget.ListView;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends Activity {
    final String LOG_TAG = "wechat_tool";
    ListView mListView;
    final int REQUEST_MANAGE_EXTERNAL_STORAGE = 1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = findViewById(R.id.listView);
        VoiceData data = VoiceData.getInstance();

        if (Environment.isExternalStorageManager()) {
            Log.d(LOG_TAG, "有权限");
            setData();
        } else {
            // 调转到申请权限页面手动开启
            Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            Uri uri = Uri.fromParts("package", getPackageName(), null);
            intent.setData(uri);
            startActivityForResult(intent, REQUEST_MANAGE_EXTERNAL_STORAGE);
        }

    }

    private void setData() {
        List<String> items = Arrays.asList("item 1", "Item 2", "Item 3");
        VoiceListAdapter adapter = new VoiceListAdapter(this, items);
        mListView.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_MANAGE_EXTERNAL_STORAGE) {
            if (Environment.isExternalStorageManager()) {
                Log.d(LOG_TAG, "有权限");
                setData();
            }
        }
    }

}
